#ifndef REGIONMAPMESSAGE_H_
#define REGIONMAPMESSAGE_H_

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include <string>
#include "ariba/ariba.h"
#include "../entities/Floor.h"
#include "../GameEngine.h"
#include "../maps/Region.h"

using namespace std;
using namespace ariba;
using std::string;

class Entity;

using_serialization;

class RegionMapMessage : public Message {
	VSERIALIZEABLE;

public:
	RegionMapMessage();
	RegionMapMessage(Entity*** region, uint32_t regionX, uint32_t regionY);
	virtual ~RegionMapMessage();
    Entity*** getMap();
    uint32_t getRegionX();
    uint32_t getRegionY();

private:
	Entity*** map;
	uint32_t regionX;
	uint32_t regionY;
};

sznBeginDefault( RegionMapMessage, X ) {
	std::stringstream ss;
	// FIXME binary_oarchive/binary_iarchive would be more efficient, but somehow doesn't work with ariba
	if (X.isSerializer()) {
		list<Entity*> map;
		for (int x = 0; x < REGION_WIDTH; x++) {
			for (int y = 0; y < REGION_HEIGHT; y++) {
				map.push_back(this->map[x][y]);
			}
		}

		boost::archive::text_oarchive oa(ss);
		oa << map;
		string s = ss.str();
		uint32_t len = s.length();
		X && regionX && regionY && len && T(s, len);
	}
	else {
		uint32_t len;
		list<Entity*> map;
		string s;
		X && regionX && regionY && len;
		X && T(s, len);
		ss << s;
		boost::archive::text_iarchive ia(ss);
		ia >> map;

		this->map = new Entity**[REGION_WIDTH];
		for (int x = 0; x < REGION_WIDTH; x++) {
			this->map[x] = new Entity*[REGION_HEIGHT];
			for (int y = 0; y < REGION_HEIGHT; y++) {
				Entity* entity = map.front();
				map.pop_front();
				this->map[x][y] = entity;
			}
		}
	}
} sznEnd();

#endif /* REGIONMAPMESSAGE_H_ */
